﻿//-----------------------------------------------------------------------------
// Filename: SctpPacketUnitTest.cs
//
// Description: Unit tests for the SctpPacket class.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
// 
// History:
// 18 Mar 2021	Aaron Clauson	Created, Dublin, Ireland.
//
// License: 
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------

using System.Linq;
using Microsoft.Extensions.Logging;
using SIPSorcery.Sys;
using Xunit;

namespace SIPSorcery.Net.UnitTests
{
    public class SctpPacketUnitTest
    {
        private ILogger logger = null;

        public SctpPacketUnitTest(Xunit.Abstractions.ITestOutputHelper output)
        {
            logger = SIPSorcery.UnitTests.TestLogHelper.InitTestLogger(output);
        }

        /// <summary>
        /// Tests that the SCTP INIT packet generated by usrsctp can be 
        /// correctly parsed.
        /// </summary>
        [Fact]
        public void ParseUsrSctpInitPacket()
        {
            byte[] buffer = {
                0xdf, 0x90, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x6a, 0xb8, 0x0e, 0x99, 0x01, 0x00, 0x00, 0xa0,
                0xe3, 0x1c, 0x55, 0x36, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x08, 0x00, 0x6c, 0xd7, 0x4d, 0xda,
                0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80,
                0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x5c, 0x91, 0x79, 0x78, 0xeb, 0xcf, 0x87, 0x25,
                0xd4, 0x64, 0xb6, 0xc3, 0x7f, 0xf9, 0x21, 0x19, 0x1d, 0x61, 0xe6, 0x31, 0x3d, 0x9d, 0x56, 0x1f,
                0xdb, 0x66, 0xb4, 0x05, 0x05, 0xd4, 0x47, 0x53, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
                0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00, 0x06,
                0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0xa0, 0x01, 0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0x70, 0x01,
                0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x89, 0x01, 0x00, 0x06, 0x00, 0x14, 0x2a, 0x02, 0x80, 0x84,
                0x69, 0x81, 0x78, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x05, 0x00, 0x08,
                0xc0, 0xa8, 0x0b, 0x32, 0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x00, 0x32 };

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0U));

            var sctpPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(57232, sctpPkt.Header.SourcePort);
            Assert.Equal(7, sctpPkt.Header.DestinationPort);
            Assert.Equal(0U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0x6ab80e99U, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.INIT; });

            var initChunk = sctpPkt.Chunks.First() as SctpInitChunk;

            Assert.Equal(0xe31c5536U, initChunk.InitiateTag);
            Assert.Equal(131072U, initChunk.ARwnd);
            Assert.Equal(10, initChunk.NumberOutboundStreams);
            Assert.Equal(2048, initChunk.NumberInboundStreams);
            Assert.Equal(1826049498U, initChunk.InitialTSN);

            foreach(var unrecognisedParam in initChunk.UnrecognizedPeerParameters)
            {
                logger.LogDebug("Chunk Unrecognised Parameter {ParameterType}.", unrecognisedParam.ParameterType);
            }
        }

        /// <summary>
        /// Tests that the SCTP INIT ACK packet generated by usrsctp can be 
        /// correctly parsed.
        /// </summary>
        [Fact]
        public void ParseUsrSctpInitAckPacket()
        {
            byte[] buffer = {
                0x00, 0x07, 0xdf, 0x90, 0xe3, 0x1c, 0x55, 0x36, 0x53, 0x72, 0xb8, 0xb5, 0x02, 0x00, 0x02, 0x50,
                0xcd, 0x6e, 0x61, 0x50, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x08, 0x00, 0x5f, 0xb4, 0x0a, 0xc2,
                0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80,
                0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0xad, 0xb8, 0x33, 0x07, 0xba, 0xb4, 0x2e, 0x59,
                0x93, 0x81, 0x50, 0x20, 0x3c, 0x70, 0xf6, 0xf1, 0x4b, 0x6f, 0x7f, 0x4e, 0x81, 0x49, 0x7c, 0x3e,
                0xef, 0xf2, 0xc8, 0x37, 0xfa, 0xd1, 0x5f, 0x74, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
                0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0xa0, 0x01,
                0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0x70, 0x01, 0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x89, 0x01,
                0x00, 0x06, 0x00, 0x14, 0x2a, 0x02, 0x80, 0x84, 0x69, 0x81, 0x78, 0x80, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x03, 0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x0b, 0x32, 0x00, 0x05, 0x00, 0x08,
                0xc0, 0xa8, 0x00, 0x32, 0x00, 0x07, 0x01, 0xb8, 0x4b, 0x41, 0x4d, 0x45, 0x2d, 0x42, 0x53, 0x44,
                0x20, 0x31, 0x2e, 0x31, 0x00, 0x00, 0x00, 0x00, 0x5d, 0x5c, 0x53, 0x60, 0xb9, 0x27, 0x08, 0x00,
                0x60, 0xea, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x55, 0x1c, 0xe3,
                0xcd, 0x6e, 0x61, 0x50, 0xc0, 0xa8, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0xdf, 0x90, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0xa0, 0xe3, 0x1c, 0x55, 0x36, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x08, 0x00,
                0x6c, 0xd7, 0x4d, 0xda, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09,
                0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x5c, 0x91, 0x79, 0x78,
                0xeb, 0xcf, 0x87, 0x25, 0xd4, 0x64, 0xb6, 0xc3, 0x7f, 0xf9, 0x21, 0x19, 0x1d, 0x61, 0xe6, 0x31,
                0x3d, 0x9d, 0x56, 0x1f, 0xdb, 0x66, 0xb4, 0x05, 0x05, 0xd4, 0x47, 0x53, 0x80, 0x04, 0x00, 0x06,
                0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x08,
                0x00, 0x05, 0x00, 0x06, 0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0xa0, 0x01, 0x00, 0x05, 0x00, 0x08,
                0xac, 0x16, 0x70, 0x01, 0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x89, 0x01, 0x00, 0x06, 0x00, 0x14,
                0x2a, 0x02, 0x80, 0x84, 0x69, 0x81, 0x78, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
                0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x0b, 0x32, 0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x00, 0x32,
                0x02, 0x00, 0x02, 0x50, 0xcd, 0x6e, 0x61, 0x50, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x08, 0x00,
                0x5f, 0xb4, 0x0a, 0xc2, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09,
                0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0xad, 0xb8, 0x33, 0x07,
                0xba, 0xb4, 0x2e, 0x59, 0x93, 0x81, 0x50, 0x20, 0x3c, 0x70, 0xf6, 0xf1, 0x4b, 0x6f, 0x7f, 0x4e,
                0x81, 0x49, 0x7c, 0x3e, 0xef, 0xf2, 0xc8, 0x37, 0xfa, 0xd1, 0x5f, 0x74, 0x80, 0x04, 0x00, 0x06,
                0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08,
                0xac, 0x16, 0xa0, 0x01, 0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0x70, 0x01, 0x00, 0x05, 0x00, 0x08,
                0xc0, 0xa8, 0x89, 0x01, 0x00, 0x06, 0x00, 0x14, 0x2a, 0x02, 0x80, 0x84, 0x69, 0x81, 0x78, 0x80,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x0b, 0x32,
                0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x00, 0x32, 0xc5, 0x35, 0x15, 0xc8, 0x35, 0x57, 0x0a, 0xd5,
                0x96, 0x29, 0xc8, 0xbf, 0x38, 0x7b, 0xc2, 0x16, 0xe9, 0x4c, 0x81, 0xbe };

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0xe31c5536U));

            var sctpPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(7, sctpPkt.Header.SourcePort);
            Assert.Equal(57232, sctpPkt.Header.DestinationPort);
            Assert.Equal(0xe31c5536U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0x5372b8b5U, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.INIT_ACK; });

            var initChunk = sctpPkt.Chunks.First() as SctpInitChunk;

            Assert.Equal(0xcd6e6150U, initChunk.InitiateTag);
            Assert.Equal(131072U, initChunk.ARwnd);
            Assert.Equal(10, initChunk.NumberOutboundStreams);
            Assert.Equal(2048, initChunk.NumberInboundStreams);
            Assert.Equal(1605634754U, initChunk.InitialTSN);
        }

        /// <summary>
        /// Tests that the SCTP COOKIE ECHO packet generated by usrsctp can be 
        /// correctly parsed. The Cookie field has been truncated for ease of testing.
        /// </summary>
        [Fact]
        public void ParseUsrSctpCookieEchoPacket()
        {
            byte[] buffer = {
                0xdf, 0x90, 0x00, 0x07, 0xcd, 0x6e, 0x61, 0x50, 0xce, 0x44, 0xba, 0xac, 0x0a, 0x00, 0x00, 0x14,
                0x4b, 0x41, 0x4d, 0x45, 0x2d, 0x42, 0x53, 0x44, 0x20, 0x31, 0x2e, 0x31, 0x00, 0x00, 0x00, 0x00 };

            // Checksum does not match because the original cookie was too long and was truncated for testing purposes.
            Assert.False(SctpPacket.VerifyChecksum(buffer, 0, buffer.Length));
            Assert.Equal(0xcd6e6150U, SctpPacket.GetVerificationTag(buffer, 0, buffer.Length));

            var sctpPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(57232, sctpPkt.Header.SourcePort);
            Assert.Equal(7, sctpPkt.Header.DestinationPort);
            Assert.Equal(0xcd6e6150U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0xce44baacU, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.COOKIE_ECHO; });

            var cookieEchoChunk = sctpPkt.Chunks.First();

            Assert.Equal(0x14, cookieEchoChunk.GetChunkLength(true));
            Assert.Equal(0x10, cookieEchoChunk.ChunkValue.Length);
        }

        /// <summary>
        /// Tests that the SCTP COOKIE ACK packet generated by usrsctp can be 
        /// correctly parsed.
        /// </summary>
        [Fact]
        public void ParseUsrSctpCookieAckPacket()
        {
            byte[] buffer = {
                0x00, 0x07, 0xdf, 0x90, 0xe3, 0x1c, 0x55, 0x36, 0xb2, 0x04, 0xdf, 0x21, 0x0b, 0x00, 0x00, 0x04 };

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0xe31c5536U));

            var sctpPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(7, sctpPkt.Header.SourcePort);
            Assert.Equal(57232, sctpPkt.Header.DestinationPort);
            Assert.Equal(0xe31c5536U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0xb204df21U, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.COOKIE_ACK; });

            var cookieAckChunk = sctpPkt.Chunks.First();

            Assert.Equal(4, cookieAckChunk.GetChunkLength(true));
        }

        /// <summary>
        /// Tests that an SCTP packet with an INIT chunk can be successfully serialised and parsed.
        /// </summary>
        [Fact]
        public void RoundTripInitPacket()
        {
            byte[] buffer = {
                0xdf, 0x90, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x6a, 0xb8, 0x0e, 0x99, 0x01, 0x00, 0x00, 0xa0,
                0xe3, 0x1c, 0x55, 0x36, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x08, 0x00, 0x6c, 0xd7, 0x4d, 0xda,
                0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80,
                0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x5c, 0x91, 0x79, 0x78, 0xeb, 0xcf, 0x87, 0x25,
                0xd4, 0x64, 0xb6, 0xc3, 0x7f, 0xf9, 0x21, 0x19, 0x1d, 0x61, 0xe6, 0x31, 0x3d, 0x9d, 0x56, 0x1f,
                0xdb, 0x66, 0xb4, 0x05, 0x05, 0xd4, 0x47, 0x53, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
                0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00, 0x06,
                0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0xa0, 0x01, 0x00, 0x05, 0x00, 0x08, 0xac, 0x16, 0x70, 0x01,
                0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x89, 0x01, 0x00, 0x06, 0x00, 0x14, 0x2a, 0x02, 0x80, 0x84,
                0x69, 0x81, 0x78, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x05, 0x00, 0x08,
                0xc0, 0xa8, 0x0b, 0x32, 0x00, 0x05, 0x00, 0x08, 0xc0, 0xa8, 0x00, 0x32 };

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0x0U));

            var initPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            var rndTripBuffer = initPkt.GetBytes();

            logger.LogDebug("Before: {BufferHexStr}", buffer.HexStr());
            logger.LogDebug("After : {RndTripBufferHexStr}", rndTripBuffer.HexStr());

            Assert.True(SctpPacket.IsValid(rndTripBuffer, 0, rndTripBuffer.Length, 0x0U));

            var sctpPkt = SctpPacket.Parse(rndTripBuffer, 0, rndTripBuffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(57232, sctpPkt.Header.SourcePort);
            Assert.Equal(7, sctpPkt.Header.DestinationPort);
            Assert.Equal(0U, sctpPkt.Header.VerificationTag);
            // Note the checksum can differ if there are any unrecognised parameters.
            Assert.Equal(0xBE4AE6F5U, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.INIT; });

            var initChunk = sctpPkt.Chunks.First() as SctpInitChunk;

            Assert.Equal(0xe31c5536U, initChunk.InitiateTag);
            Assert.Equal(131072U, initChunk.ARwnd);
            Assert.Equal(10, initChunk.NumberOutboundStreams);
            Assert.Equal(2048, initChunk.NumberInboundStreams);
            Assert.Equal(1826049498U, initChunk.InitialTSN);
        }

        /// <summary>
        /// Tests that the SCTP HEARTBEAT packet generated by usrsctp can be 
        /// correctly parsed.
        /// </summary>
        [Fact]
        public void ParseUsrSctpHeartbeatPacket()
        {
            byte[] buffer = {
                0x00, 0x07, 0x11, 0x5c, 0x05, 0x4a, 0x3a, 0xf0, 0x02, 0x3b, 0xcc, 0x90, 0x04, 0x00, 0x00, 0x2c,
                0x00, 0x01, 0x00, 0x28, 0x4c, 0x3a, 0x56, 0x60, 0xa8, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0x054a3af0U));

            var sctpPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(7, sctpPkt.Header.SourcePort);
            Assert.Equal(4444, sctpPkt.Header.DestinationPort);
            Assert.Equal(0x054a3af0U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0x023bcc90U, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.HEARTBEAT; });

            var heartbeatChunk = sctpPkt.Chunks.First();

            Assert.Equal(44, heartbeatChunk.GetChunkLength(true));
        }

        /// <summary>
        /// Tests that an SCTP packet with a HEARTBEAT chunk can be successfully serialised and parsed.
        /// </summary>
        [Fact]
        public void RoundTripHeartbeatPacket()
        {
            byte[] buffer = {
                0x00, 0x07, 0x11, 0x5c, 0x05, 0x4a, 0x3a, 0xf0, 0x02, 0x3b, 0xcc, 0x90, 0x04, 0x00, 0x00, 0x2c,
                0x00, 0x01, 0x00, 0x28, 0x4c, 0x3a, 0x56, 0x60, 0xa8, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0x054a3af0U));

            var heartbeatPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            var rndTripBuffer = heartbeatPkt.GetBytes();

            logger.LogDebug("Before: {BufferHexStr}", buffer.HexStr());
            logger.LogDebug("After : {RndTripBufferHexStr}", rndTripBuffer.HexStr());

            Assert.True(SctpPacket.IsValid(rndTripBuffer, 0, rndTripBuffer.Length, 0x054a3af0U));

            var sctpPkt = SctpPacket.Parse(rndTripBuffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(7, sctpPkt.Header.SourcePort);
            Assert.Equal(4444, sctpPkt.Header.DestinationPort);
            Assert.Equal(0x054a3af0U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0x023bcc90U, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.HEARTBEAT; });

            var heartbeatChunk = sctpPkt.Chunks.First();

            Assert.Equal(44, heartbeatChunk.GetChunkLength(true));
        }

        /// <summary>
        /// Tests that the SCTP DATA packet generated by usrsctp can be 
        /// correctly parsed.
        /// </summary>
        [Fact]
        public void ParseUsrSctpDataPacket()
        {
            byte[] buffer = {
                0x00, 0x07, 0x11, 0x5c, 0x2e, 0x0b, 0x82, 0xc7, 0x5d, 0x2c, 0xeb, 0xa7, 0x00, 0x07, 0x00, 0x13,
                0xdf, 0x08, 0xc1, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x69, 0x0a, 0x00};

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0x2e0b82c7U));

            var sctpPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(7, sctpPkt.Header.SourcePort);
            Assert.Equal(4444, sctpPkt.Header.DestinationPort);
            Assert.Equal(0x2e0b82c7U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0x5d2ceba7U, sctpPkt.Header.Checksum);

            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.DATA; });

            var dataChunk = sctpPkt.Chunks.First() as SctpDataChunk;

            Assert.Equal(20, dataChunk.GetChunkLength(true));
            Assert.Equal("68690A", dataChunk.UserData.HexStr());
            Assert.Equal(3741893047, dataChunk.TSN);
        }

        /// <summary>
        /// Tests that an SCTP packet with a DATA chunk can be successfully serialised and parsed.
        /// </summary>
        [Fact]
        public void RoundTripDataPacket()
        {
            byte[] buffer = {
                0x00, 0x07, 0x11, 0x5c, 0x2e, 0x0b, 0x82, 0xc7, 0x5d, 0x2c, 0xeb, 0xa7, 0x00, 0x07, 0x00, 0x13,
                0xdf, 0x08, 0xc1, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x69, 0x0a, 0x00};

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0x2e0b82c7U));

            var dataPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            var rndTripBuffer = dataPkt.GetBytes();

            logger.LogDebug("Before: {BufferHexStr}", buffer.HexStr());
            logger.LogDebug("After : {RndTripBufferHexStr}", rndTripBuffer.HexStr());

            Assert.True(SctpPacket.IsValid(rndTripBuffer, 0, rndTripBuffer.Length, 0x2e0b82c7U));

            var sctpPkt = SctpPacket.Parse(rndTripBuffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(7, sctpPkt.Header.SourcePort);
            Assert.Equal(4444, sctpPkt.Header.DestinationPort);
            Assert.Equal(0x2e0b82c7U, sctpPkt.Header.VerificationTag);
            Assert.Equal(0x5d2ceba7U, sctpPkt.Header.Checksum);
            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.DATA; });

            var dataChunk = sctpPkt.Chunks.First() as SctpDataChunk;

            Assert.Equal(20, dataChunk.GetChunkLength(true));
            Assert.Equal("68690A", dataChunk.UserData.HexStr());
            Assert.Equal(3741893047, dataChunk.TSN);
        }

        /// <summary>
        /// Tests that the SCTP ABORT packet generated by usrsctp can be 
        /// correctly parsed.
        /// </summary>
        [Fact]
        public void ParseUsrSctpAbortPacket()
        {
            logger.LogDebug("--> {MethodName}", System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            byte[] buffer = {
                0x00, 0x07, 0x11, 0x5c, 0x93, 0xc9, 0xd9, 0x8a, 0x19, 0x82, 0x31, 0xc1, 0x06, 0x00, 0x00, 0x3b,
                0x00, 0x0d, 0x00, 0x37, 0x43, 0x75, 0x6d, 0x20, 0x61, 0x63, 0x6b, 0x20, 0x30, 0x30, 0x30, 0x30,
                0x30, 0x30, 0x30, 0x30, 0x20, 0x67, 0x72, 0x65, 0x61, 0x74, 0x65, 0x72, 0x20, 0x6f, 0x72, 0x20,
                0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x54, 0x53, 0x4e, 0x20, 0x63,
                0x36, 0x65, 0x33, 0x61, 0x64, 0x33, 0x63, 0x00};

            Assert.True(SctpPacket.IsValid(buffer, 0, buffer.Length, 0x93c9d98aU));

            var sctpPkt = SctpPacket.Parse(buffer, 0, buffer.Length);

            Assert.NotNull(sctpPkt);
            Assert.Equal(7, sctpPkt.Header.SourcePort);
            Assert.Equal(4444, sctpPkt.Header.DestinationPort);
            Assert.Equal(0x93c9d98aU, sctpPkt.Header.VerificationTag);
            Assert.Equal(0x198231c1U, sctpPkt.Header.Checksum);

            Assert.Single(sctpPkt.Chunks);
            Assert.Contains(sctpPkt.Chunks, (chunk) => { return chunk.KnownType == SctpChunkType.ABORT; });

            var abortChunk = sctpPkt.Chunks.First() as SctpAbortChunk;
            var cause = (SctpErrorProtocolViolation)(abortChunk.ErrorCauses.First());

            logger.LogDebug("ABORT reason={Reason}.", cause.AdditionalInformation);

            Assert.Single(abortChunk.ErrorCauses);
            Assert.Equal("Cum ack 00000000 greater or equal than TSN c6e3ad3c", cause.AdditionalInformation);
        }
    }
}
